package cn.ddiancan.xddcloud.psc.base.service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.ddiancan.xddcloud.common.entity.BaseVO;
import cn.ddiancan.xddcloud.common.entity.CommonResult;
import cn.ddiancan.xddcloud.common.entity.UserVO;
import cn.ddiancan.xddcloud.psc.base.domain.CkMerchantEntity;
import cn.ddiancan.xddcloud.psc.base.feign.MerchantRemoteClientService;
import cn.ddiancan.xddcloud.psc.base.feign.UserRemoteClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;

@Service
public class UserClientService {

    @Autowired
    private UserRemoteClientService userRemoteClientService;

    @Autowired
    private MerchantRemoteClientService merchantRemoteClientService;

    /**
     * 通过id批量获取用户
     *
     * @param ids ids
     * @return CommonResult<List < UserVO>>
     */
    public CommonResult<List<UserVO>> getUserByIds(List<String> ids) {
        return userRemoteClientService.getUserByIds(ids);
    }

    /**
     * 填充创建人、更新人名称
     *
     * @param list list
     */
    public void populateCreateUpdateName(List<? extends BaseVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Set<String> ids = new HashSet<>();
        List<String> listCreatedBy = list.stream().map(BaseVO::getCreatedBy).filter(Objects::nonNull).toList();
        List<String> listUpdatedBy = list.stream().map(BaseVO::getLastUpdateBy).filter(Objects::nonNull).toList();
        ids.addAll(listCreatedBy);
        ids.addAll(listUpdatedBy);
        CommonResult<List<UserVO>> userByIds = getUserByIds(ids.stream().toList());
        if (Objects.nonNull(userByIds) && Objects.nonNull(userByIds.getData())) {
            Map<String, UserVO> userIdMap =
                userByIds.getData().stream().collect(Collectors.toMap(UserVO::getUserId, Function.identity()));
            list.forEach(vo -> {
                vo.setCreateName(
                    Optional.ofNullable(userIdMap.get(vo.getCreatedBy())).orElse(new UserVO()).getUserName());
                vo.setLastUpdateName(
                    Optional.ofNullable(userIdMap.get(vo.getCreatedBy())).orElse(new UserVO()).getUserName());
            });
        }
    }

    /**
     * 通过Id获取用户
     *
     * @param userId userId
     * @return CommonResult<UserVO>
     */
    public CommonResult<UserVO> getUserById(String userId) {
        return userRemoteClientService.loadUserById(userId);
    }

    /**
     * 通过userAccount获取用户
     *
     * @param userAccount userAccount
     * @return CommonResult<UserVO>
     */
    public CommonResult<UserVO> getUserByAccount(String userAccount) {
        return userRemoteClientService.loadUserByAccount(userAccount);
    }

    /**
     * 通过userAccount获取用户及权限
     *
     * @param userAccount userAccount
     * @return CommonResult<UserVO>
     */
    public CommonResult<UserVO> getUserPermissionByAccount(String userAccount) {
        return userRemoteClientService.loadUserPermissionByAccount(userAccount);
    }

    /**
     * 通过userAccount获取用户及权限，用于顶顶登录
     *
     * @param tempAuthCode tempAuthCode
     * @return CommonResult<UserVO>
     */
    public CommonResult<UserVO> getUserPermissionByTempAuthCode(String tempAuthCode) {
        return userRemoteClientService.loadUserPermissionByTempAuthCode(tempAuthCode);
    }

    /**
     * 调用ai生成问候语
     *
     * @param words words
     * @return CommonResult<String>
     */
    public CommonResult<String> doChat(String words) {
        return userRemoteClientService.doChat(words);
    }

    /**
     * 通过手机号查询 商户
     * @param mobile
     * @return
     */
    public CommonResult<CkMerchantEntity> queryByMobile( String mobile){
        return merchantRemoteClientService.queryByMobile(mobile);
    }
}
